package org.springmole.common.service.impl;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.springmole.common.GeneralXMLException;
import org.springmole.common.service.GeneralXMLService;
import org.w3c.dom.Document;


public class GeneralXMLServiceImpl implements GeneralXMLService {

	private static DocumentBuilderFactory documentBuilderFactory = null;

	@Override
	public Document createDocument() throws GeneralXMLException {
		return getDocumentBuilder().newDocument();
	}

	@Override
	public DocumentBuilderFactory getDocumentBuilderFactory() {
		if (documentBuilderFactory == null) {
			documentBuilderFactory = DocumentBuilderFactory.newInstance();
		}
		return documentBuilderFactory;
	}

	@Override
	public DocumentBuilder getDocumentBuilder() throws GeneralXMLException {
		try {
			return getDocumentBuilderFactory().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			throw convertXMLException(e);
		}
	}


	protected GeneralXMLException convertXMLException(Exception exception) {
		//if (exception instanceof ) {
		//}
		return new GeneralXMLException(exception);
	}
	
	
}
